//
//  CustomConnectView.swift
//  BDBoxDemo
//
//  Created by QZBD on 2017/12/5.
//  Copyright © 2017年 BDBoxSDK. All rights reserved.
//

import UIKit
import BDBox
import CoreBluetooth

class CustomConnectView: UITableViewController {

    var peripherals:[CBPeripheral] =  [CBPeripheral]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "自定义连接盒子视图"
        
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        Bluetooth.scanForPeripherals {[unowned self] (peripherals) in
            self.peripherals = peripherals;
            self.tableView.reloadData()
        }
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.peripherals.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

        cell.textLabel?.text = self.peripherals[indexPath.row].name;

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        Bluetooth.connectPeripheral(self.peripherals[indexPath.row])
    }

}
